package com.ts.graphql.client;

import java.util.*;
import java.util.stream.Collectors;

/**
 *
 * 需要返回的字段
 *
 * @author suyi
 */
public class ReturnAttributes {

    private String name;

    protected List<ReturnAttributes> children;

    public ReturnAttributes(String name) {
        this.name = name;
    }

    public ReturnAttributes(String name, boolean leaf) {
        this.name = name;
        if (! leaf) {
            children = new ArrayList<>();
        }
    }

    public ReturnAttributes addChild(ReturnAttributes returnAttributes) {
        if (children == null) {
            throw new UnsupportedOperationException();
        }
        children.add(returnAttributes);
        return this;
    }

    public ReturnAttributes addChildren(Collection<ReturnAttributes> returnAttributes) {
        if (children == null) {
            throw new UnsupportedOperationException();
        }
        children.addAll(returnAttributes);
        return this;
    }

    @Override
    public String toString() {
        StringJoiner stringJoiner = new StringJoiner(" ");
        stringJoiner.add(this.name);
        if (children != null) {
            stringJoiner.add("{");
            for (ReturnAttributes child : children) {
                stringJoiner.add(child.toString());
            }
            stringJoiner.add("}");
        }
        return stringJoiner.toString();
    }

}
